cmake_minimum_required(VERSION 3.24)

include(Toolchain/arm-none-eabi-gcc.cmake)

project(STM32F103C8T6)
message("Build type: " ${CMAKE_BUILD_TYPE})

enable_language(C CXX)

add_executable(${PROJECT_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES
    C_STANDARD 23
    C_STANDARD_REQUIRED ON
    C_EXTENSIONS OFF
    CXX_STANDARD 23
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
)
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

add_subdirectory(Driver)

target_sources(
    ${PROJECT_NAME} PRIVATE
    User/src/main.c
    User/src/stm32f10x_it.c
    User/src/syscalls.c
    User/src/sysmem.c
    User/src/system_stm32f10x.c
)
target_include_directories(${PROJECT_NAME} PRIVATE
    User/inc
)
target_link_libraries(${PROJECT_NAME} PRIVATE
    STM32Drivers
)
list(REMOVE_ITEM CMAKE_C_IMPLICIT_LINK_LIBRARIES ob)

# 生成hex和bin文件
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${PROJECT_NAME}> $<TARGET_FILE_DIR:${PROJECT_NAME}>/${PROJECT_NAME}.hex
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${PROJECT_NAME}> $<TARGET_FILE_DIR:${PROJECT_NAME}>/${PROJECT_NAME}.bin
    COMMENT "Building ${PROJECT_NAME}.hex and ${PROJECT_NAME}.bin"
)
